<!--
 * @Description: 
 * @Date: 2020-12-07 10:50:53
 * @LastEditors: lother
 * @LastEditTime: 2021-01-12 15:07:18
 * @FilePath: \AccountOpening-H5\src\App.vue
-->
<template>
  <div id="app">
    <!-- <van-nav-bar safe-area-inset-top /> -->
    <router-view  v-if="isRouterAlive" />
    <van-number-keyboard safe-area-inset-bottom />
  </div>
</template>
<script>
export default {
    provide() {
      //父组件中通过provide来提供变量，在子组件中通过inject来注入变量。
      return {
        StepsPage:()=> this.page_flow,
        user_fill:() => this.user_fill,
        page_info:()=> this.page_info,
        branch:()=> this.branch,
        user_info:()=>this.user_info,
        onload: this.onload 
      };
    },
    data() {
      return {
        page_flow:{},
        user_fill:{},
        page_info:{},
        branch:{},
        user_info:{},
        isRouterAlive: true   
      }
    },
    computed:{
      langSuff(){
          return  this.$store.state.langSuff
      },
      titleFirst(){
        return JSON.parse(localStorage.getItem('common'))['terminal_'+this.langSuff]
      }
    },
    mounted(){
      this.$Icon()
      // console.log(this.$t('downLoads'),this.$route.meta.metaTitle)
      // document.title  = this.$t(this.$route.meta.metaTitle)
      if (window.history && window.history.pushState) {
      //   history.pushState(null, null, document.URL);
        // window.addEventListener("popstate", this.cancel, false);
      //   // window.addEventListener("onpopstate", this.cancel, false);
      }
    },
    methods: {
      reload() {
        this.user_fill={}
        this.page_flow={}
        this.page_info={}
        this.branch={}
        this.user_info={}
        this.$pageFlow()
        .then(res=>{
          if(res.code==1){
            // this.page_flow=res.data.page_flow
            var index = res.data.page_flow.findIndex((item) => item == res.data.page_info.id)
            this.page_flow = {
              prev_id: res.data.page_flow[index-1]??'',
              curr_id:res.data.page_flow[index],
              next_id:res.data.page_flow[index+1]??'',
              index:index,
              total:(res.data.page_flow.length)
            }
            // if(JSON.stringify(res.data.user_fill)=='{}'){
              // }
            this.pageFor(res.data.page_info.fields,res.data.user_fill)
            this.page_info = res.data.page_info;
            if(process.env.NODE_ENV  === 'production'){//生产环境开启
              if(this.page_info.type==2 && this.$route.name !=='custom'){
                   this.$router.replace({
                     path:'custom',
                      query:{id:this.page_info.id}
                  })
              }else if(this.page_info.type==1){
                  if(!['verification?type=web','verification?type=local',
                        'CounterAddress','CounterOrder','OnlineAddress','OnlineRemit','verification'].includes(this.page_info.url_path)){
                    this.$routerTo(this.page_info.url_path)
                  }
              }
            }
            this.branch=  res.data.page_info?.branch ?? {}
             this.user_info=res.data.user_info;
             localStorage.setItem('StepsPage',JSON.stringify(this.page_flow))
              document.title = this.titleFirst+'-'+ this.page_info['name_'+this.langSuff]
          }
        })
      },
       onload () {
          this.isRouterAlive = false;            //先关闭，
          this.$nextTick(function () {
              this.isRouterAlive = true;         //再打开
          }) 
      },
      pageFor(list,echoList){
          var form={}
        if(!['CounterOrder','OnlineRemit'].includes(this.$route.name)){
          for (let index = 0; index < list.length; index++) {
            const element = list[index];
            if(!['row','firstTitle','editor','textarea'].includes(element.type)){
              if(form[element.name]){
                form[element.name] = element.type=='switch'?false:''
              }
            }else if(['row'].includes(element.type)){
                this.pageFor(element.container,echoList)
            }
          }
        }
        // form  =  initMenu.splice(initMenu.findIndex(item => item === undefined), 1)
        this.user_fill = Object.assign({},this.user_fill,form,echoList)
        localStorage.setItem('user_fill',JSON.stringify(this.user_fill))
       
      },
      cancel: function() {
        alert('fansheng')
      },
    },
    watch:{
      $route(to){
        document.title = this.titleFirst+'-'+ this.$t(to.meta.metaTitle)
        // history.pushState({url: to.fullPath,page:this.StepsPage.currPage} )
        if(!['documentType','download','pact','examine','pactList','verify','login','404','autoImg'].includes(to.name)){
            this.reload()
        }
      },
    }
}
</script>